package com.ithm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithm.reggie.common.CustomException;
import com.ithm.reggie.dto.DishDto;
import com.ithm.reggie.dto.SetmealDto;
import com.ithm.reggie.entity.Dish;
import com.ithm.reggie.entity.DishFlavor;
import com.ithm.reggie.entity.Setmeal;
import com.ithm.reggie.entity.SetmealDish;
import com.ithm.reggie.service.SetmealDishService;
import com.ithm.reggie.service.SetmealService;
import com.ithm.reggie.mapper.SetmealMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86182
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2024-08-10 12:10:34
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{
    @Autowired SetmealDishService setmealDishService;

    /**
     * 保存套餐信息到setmeal表和setmeal_dish表中
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时删除相应setmeal_dish表中的数据
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        int count = this.count(setmealLambdaQueryWrapper);
        if(count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //删除dish表里面的相应菜品,也就是更新deleted字段
        LambdaUpdateWrapper<Setmeal> dishLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        dishLambdaUpdateWrapper.in(Setmeal::getId,ids).set(Setmeal::getIsDeleted,1);
        this.update(dishLambdaUpdateWrapper);

        //删除flavor表里面相应记录,也就是更新deleted字段
        LambdaUpdateWrapper<SetmealDish> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(SetmealDish::getDishId, ids).set(SetmealDish::getIsDeleted,1);
        setmealDishService.update(lambdaUpdateWrapper);
    }

    /**
     * 修改套餐，同时setmeal_dish表也做修改
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //保存菜品到dish表
        this.updateById(setmealDto);

        //将原来的口味删除
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lambdaQueryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 修改套餐之前先回显数据
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        Setmeal setmeal = this.getById(id);

        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());

        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);

        SetmealDto dishDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,dishDto);
        dishDto.setSetmealDishes(list);

        return dishDto;
    }
}




